package com.lin.utils.treeuntil;

import com.lin.dataobject.TSRegion;
import com.lin.utils.treenode.RegionTreeNode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/8/2.
 */
public class RegionTreeUntil {
    public List fillTree(List<TSRegion> institutions){
        List<RegionTreeNode> list=new ArrayList<RegionTreeNode>();
        Map map=new HashMap<String, RegionTreeNode>();
        try {
            for(TSRegion institution:institutions){
                RegionTreeNode node=new RegionTreeNode();
                node.setId(institution.getId());
                node.setText(institution.getName());
                node.setName_en(institution.getNameEn());
                if(!institution.getPid().equals("0")){
                    node.setParentId(institution.getPid());
                }
                map.put(institution.getId(), node);
            }
            for(TSRegion institution:institutions){
                if(!institution.getPid().equals("0")){
                    if(institution.getPid().equals("0")){
                        list.add((RegionTreeNode)map.get(institution.getId()));
                    }else{
                        String pId=institution.getPid();
                        RegionTreeNode pNode=(RegionTreeNode)map.get(pId);
                        RegionTreeNode cNode=(RegionTreeNode)map.get(institution.getId());
                        pNode.addChild(cNode);
                    }
                }else {
                    list.add((RegionTreeNode)map.get(institution.getId()));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }
}
